package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.ScStationColumnSecond;
import com.huaxin.hxcloud.R;
import com.huaxindata.common.util.HxLogUtils;

import java.util.List;

/**
 * @author songxf
 * @date 2018/5/18
 * @Desc 首创站点树形结构adapter
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxScStaionListAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    //数据第一层
    public static final int ColumnStationFirst = 0;
    //数据中间层
    public static final int ColumnStation      = 10;
    //数据最里层
    public static final int ColumnStationEnd   = 20;
    public              int padding            = 40;
    private final Context mContext;
    //层级数
    private       int     mStationColumnLevel;

    public HxScStaionListAdapter(List<MultiItemEntity> data, Context context) {
        super(data);
        this.mContext = context;
        addItemType(ColumnStationFirst, R.layout.item_station_first);
        addItemType(ColumnStation, R.layout.item_station_second);
        addItemType(ColumnStationEnd, R.layout.item_station_end);
    }

    @Override
    protected void convert(final BaseViewHolder holder, MultiItemEntity multiItemEntity) {
        switch (holder.getItemViewType()) {
            case ColumnStationFirst:
                //第一层级
                final ScStationColumnSecond stationColumnFirst = (ScStationColumnSecond) multiItemEntity;
                TextView tvfirst = holder.getView(R.id.tv_first);
                //
                tvfirst.setPadding(padding, 0, 0, 0);
                holder.setText(R.id.tv_first, stationColumnFirst.getText() + " " + stationColumnFirst.getLevel());
                //箭头
                ImageView ivExpandArrow = holder.getView(R.id.img_expand_arrow);
                if (stationColumnFirst.getChildren() == null) {
                    ivExpandArrow.setVisibility(View.GONE);
                } else {
                    ivExpandArrow.setVisibility(View.VISIBLE);
                }
                tvfirst.setText(stationColumnFirst.getText());
                if (stationColumnFirst.isExpanded()) {
                    holder.setImageResource(R.id.img_expand_arrow, R.drawable.arrow_r);
                } else {
                    holder.setImageResource(R.id.img_expand_arrow, R.drawable.arrow_b);
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (stationColumnFirst.getSubItems() == null || stationColumnFirst.getSubItems().size() == 0) {
                            //  ToastUtil.showToast(HxStationColumnActivity.this,"no column next");
                            return;
                        }
                        if (stationColumnFirst.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }
                        notifyDataSetChanged();
                    }
                });

                break;
            case ColumnStation:
                //TODO 第二 三 四 五......层级
                final ScStationColumnSecond stationColumn = (ScStationColumnSecond) multiItemEntity;
                TextView tvSecond = holder.getView(R.id.tv_second);
                mStationColumnLevel = stationColumn.getLevel();
                tvSecond.setPadding(padding * stationColumn.getLevel(), 0, 0, 0);
                //箭头
                ImageView ivExpandArrowSecond = holder.getView(R.id.iv_expand_arrow);

                tvSecond.setText(stationColumn.getText());
                if (stationColumn.isExpanded()) {
                    holder.setImageResource(R.id.iv_expand_arrow, R.drawable.arrow_r);
                } else {
                    holder.setImageResource(R.id.iv_expand_arrow, R.drawable.arrow_b);
                }
                if (stationColumn.getChildren() == null) {
                    ivExpandArrowSecond.setVisibility(View.GONE);
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getAdapterPosition();
                        if (stationColumn.getChildren() == null) {
                            intentStationList(stationColumn.getId().substring(4), stationColumn.getText());
                        }
                        notifyDataSetChanged();

                        if (stationColumn.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                        }

                    }
                });
                break;
            case ColumnStationEnd:
                //TODO 最后一层
                final ScStationColumnSecond stationColumn3 = (ScStationColumnSecond) multiItemEntity;
                TextView tvSecondEnd = holder.getView(R.id.tv_second);
                holder.getView(R.id.tv_station_falg).setPadding(padding * (mStationColumnLevel + 1), 0, 0, 0);
                tvSecondEnd.setText(stationColumn3.getText());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        intentStationList(stationColumn3.getId().substring(4), stationColumn3.getText());
                    }
                });
                break;
        }
    }

    private void intentStationList(String id, String title) {
        Intent intent = new Intent(mContext, HxScStationListActivity.class);
        HxLogUtils.i("stationCode3---->" + id);
        intent.putExtra(Constants.IK_STATION_CODE, id);
        intent.putExtra(Constants.IK_STATION_NAME, title);
        mContext.startActivity(intent);
    }
}
